package view;

import java.awt.Font;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import dao.*;
import dao.Student;
public class JFreeChartTest2 extends ApplicationFrame
{
	       	        
    public JFreeChartTest2(String title) throws SQLException
    {
        super(title);
        this.setContentPane(createPanel()); //构造函数中自动创建Java的panel面板
    }
    
    public static CategoryDataset createDataset() throws SQLException //创建柱状图数据集
    {
    	Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        
        
        String sql ="select * from students";
        conn = JDBCUtil.getConnection();
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
            
        /*分别存放三科成绩*/
        List<Double> jscore = new ArrayList<Double>();
        List<Double> mscore = new ArrayList<Double>();
        List<Double> escore = new ArrayList<Double>();
        
        int[] a = new int[6];  //存放Java成绩分段(A~F)
        int[] b = new int[6];  //存放Math成绩分段(A~F)
        int[] c = new int[6];  //存放Eenglish成绩分段(A~F)
        while(rs.next()){
        	double sa = rs.getDouble("javaScore");
         	jscore.add(sa);
          	judge(sa,a);
        	
         	double sb = rs.getDouble("mathScore");
           	mscore.add(sb);
           	judge(sb,b);
           	
           	double sc = rs.getDouble("englishScore");
           	escore.add(sc);
           	judge(sc,c);
               
       }
       //用户选择Java成绩柱状图
        DefaultCategoryDataset dataset0 = null;
        String choice1 = "java";//随便定了一个判断条件，整合时修改
        
        
        if(choice1.equals("java"))
        	dataset0 = dataset(a);
        else if(choice1.equals("math")) 
        	dataset0 = dataset(b);  //选择数学成绩柱状图
        else if(choice1.equals("english"))
        	dataset0 = dataset(c);  //选择英语成绩柱状图
        
        return dataset0;
        
        
    }
    
    
    /**
     * 
     * 数据与图表位置对应
     */
    private static DefaultCategoryDataset dataset(int[] arr) {
    	DefaultCategoryDataset dataset=new DefaultCategoryDataset();
        dataset.setValue(arr[0],"a","0~50");
        dataset.setValue(arr[1],"b","50~60");
        dataset.setValue(arr[2],"c","60~70");
        dataset.setValue(arr[3],"d","70~80");
        dataset.setValue(arr[4],"e","80~90");
        dataset.setValue(arr[5],"f","90~100");
        return dataset;
		
	}
    
    /**
     * 
     * 将成绩分段
     */
	private static void judge(double score, int[] arr) {
		if(score>=0&&score<50)
			arr[0]++;
		else if(score>=50&&score<60)
			arr[1]++;
		else if(score>=60&&score<70)
			arr[2]++;
		else if(score>=70&&score<80)
			arr[3]++;
		else if(score>=80&&score<90)
			arr[4]++;
		else if(score>=90&&score<=100)
			arr[5]++;
	}
	
	
	/**
     * 
     * 用数据集创建一个图表
     */
	public static JFreeChart createChart(CategoryDataset dataset) //用数据集创建一个图表
    {
        JFreeChart chart=ChartFactory.createBarChart("hi", "成绩情况", 
                "人数", dataset, PlotOrientation.VERTICAL, true, true, false); //创建一个JFreeChart
        chart.setTitle(new TextTitle("学生成绩统计",new Font("宋体",Font.BOLD+Font.ITALIC,20)));//设置主标题
        CategoryPlot plot=(CategoryPlot)chart.getPlot();//获得图标中间部分，即plot
        CategoryAxis categoryAxis=plot.getDomainAxis();//获得横坐标
        categoryAxis.setLabelFont(new Font("微软雅黑",Font.BOLD,12));//设置横坐标字体
        return chart;
    }
    
    public static JPanel createPanel() throws SQLException
    {
        JFreeChart chart =createChart(createDataset());
        return new ChartPanel(chart); //将chart对象放入Panel面板中去，ChartPanel类已继承Jpanel
    }
    
}